﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Api.Common
{
    /// <summary>
    /// 生成 Ajax 响应消息 字符串 供Ajax請求使用
    /// 数据格式：{"status":1,"msg":"出错啦~~","data":[{},{}],"url":"Login.aspx"}
    /// </summary>
    public class AjaxMsgHelper
    {
        #region 输出 狀態為成功的 Json字符串
        /// <summary>
        /// 输出 狀態為成功的 Json字符串
        /// </summary>
        /// <returns></returns>
        public static void ResponseSuccess()
        {
            Response(true, null, null, null);
        }

        /// <summary>
        /// 输出 狀態為成功的 Json字符串
        /// </summary>
        /// <param name="objData">数据</param>
        /// <returns></returns>
        public static void ResponseSuccess(object objData)
        {
            Response(true, null, objData, null);
        }

        /// <summary>
        /// 输出 狀態為成功的 Json字符串
        /// </summary>
        /// <param name="msg">消息</param>
        public static void ResponseSuccess(string msg)
        {
            Response(true, msg, null, null);
        }

        /// <summary>
        /// 输出 狀態為成功的 Json字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="objData">数据</param>
        /// <returns></returns>
        public static void ResponseSuccess(string msg, object objData)
        {
            Response(true, msg, objData, null);
        }

        /// <summary>
        /// 输出 狀態為成功的 Json字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="objData">数据</param>
        /// <param name="url">跳轉網址</param>
        /// <returns></returns>
        public static void ResponseSuccess(string msg, object objData, string url)
        {
            Response(true, msg, objData, url);
        }
        #endregion

        #region 输出 狀態為失敗的 Json字符串
        /// <summary>
        /// 输出 狀態為失敗的 Json字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static void ResponseFail(string msg)
        {
            Response(false, msg, null, null);
        }

        /// <summary>
        /// 输出 狀態為失敗的 Json字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="objData">数据</param>
        /// <returns></returns>
        public static void ResponseFail(string msg, object objData)
        {
            Response(false, msg, objData, null);
        }

        /// <summary>
        /// 输出 狀態為失敗的 Json字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="objData">数据</param>
        /// <param name="url">跳轉網址</param>
        /// <returns></returns>
        public static void ResponseFail(string msg, object objData, string url)
        {
            Response(false, msg, objData, url);
        }
        #endregion

        #region 输出Json字符串
        /// <summary>
        /// 输出Json字符串
        /// </summary>
        /// <param name="strJson">Json字符串</param>
        /// <returns></returns>
        public static void ResponseJson(string strJson)
        {
            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Write(strJson);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 输出Json字符串
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static void Response(bool status, string msg)
        {
            Response(status, msg, null, null);
        }

        /// <summary>
        /// 输出Json字符串
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <param name="objData">数据</param>
        /// <returns></returns>
        public static void Response(bool status, string msg, object objData)
        {
            Response(status, msg, objData, null);
        }

        /// <summary>
        /// 输出Json字符串
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <param name="objData">数据</param>
        /// <param name="url">跳轉網址</param>
        /// <param name="isLogin"></param>
        /// <returns></returns>
        public static void Response(bool status, string msg, object objData, string url, bool isLogin = true)
        {
            try
            {
                var isoConverter = new IsoDateTimeConverter();
                isoConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                AjaxResult result = new AjaxResult()
                {
                    status = status,
                    msg = msg ?? string.Empty,
                    data = objData,
                    url = url ?? string.Empty,
                    islogin = isLogin,
                };
                var strJson = JsonConvert.SerializeObject(result, isoConverter);
                HttpContext.Current.Response.ContentType = "application/json";
                HttpContext.Current.Response.Write(strJson);
            }
            finally
            {
                HttpContext.Current.Response.End();
            }
        }
        #endregion
    }
}
